<!DOCTYPE html><html lang="zh"><head><meta name="generator" content="Hexo 3.8.0"><meta charset="utf-8"><title>Elasticsearch初探 - i蝸居年華_谢谢谢_CODE_HOME</title><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta name="description" content="[TOC] 前言最近这个好火, 简单体验下"><meta name="keywords" content="Elasticsearch,ELK"><meta property="og:type" content="article"><meta property="og:title" content="Elasticsearch初探"><meta property="og:url" content="https://xiefayang.com/2018/01/01/Elasticsearch初探/index.html"><meta property="og:site_name" content="i蝸居年華_谢谢谢_CODE_HOME"><meta property="og:description" content="[TOC] 前言最近这个好火, 简单体验下"><meta property="og:locale" content="zh-CN"><meta property="og:image" content="https://blog-md-pic-1259135436.cos.ap-chengdu.myqcloud.com/ELK/es%E5%B8%A6%E7%B4%A2%E5%BC%95%E7%9A%84%E9%9B%86%E7%BE%A4.png"><meta property="og:updated_time" content="2019-04-29T06:44:29.557Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Elasticsearch初探"><meta name="twitter:description" content="[TOC] 前言最近这个好火, 简单体验下"><meta name="twitter:image" content="https://blog-md-pic-1259135436.cos.ap-chengdu.myqcloud.com/ELK/es%E5%B8%A6%E7%B4%A2%E5%BC%95%E7%9A%84%E9%9B%86%E7%BE%A4.png"><link rel="icon" href="/images/favicon.svg"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css"><link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.4.1/css/all.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/atom-one-dark.css"><style>body>.footer,body>.navbar,body>.section{opacity:0}</style><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css"><link rel="stylesheet" href="/css/back-to-top.css"><script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="//hm.baidu.com/hm.js?b59777544dd5d59ce94e191bac62427c";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><link rel="stylesheet" href="/css/progressbar.css"><script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><link rel="stylesheet" href="/css/style.css"></head><body class="is-3-column"><nav class="navbar navbar-main"><div class="container"><div class="navbar-brand is-flex-center"> <a class="navbar-item navbar-logo" href="/"><img src="/images/maotouying.svg" alt="Elasticsearch初探" height="28"> <span style="font-size:16px;display:inline-block;vertical-align:top;font-weight:600">i蝸居年華_谢谢谢</span></a></div><div class="navbar-menu"><div class="navbar-start"> <a class="navbar-item" href="/">首页</a> <a class="navbar-item" href="/archives">归档</a> <a class="navbar-item" href="/categories">分类</a> <a class="navbar-item" href="/tags">标签</a> <a class="navbar-item" href="/about">关于</a></div><div class="navbar-end"><a class="navbar-item is-hidden-tablet catalogue" title="文章目录" href="javascript:;"><i class="fas fa-list-ul"></i></a><a class="navbar-item search" title="搜索" href="javascript:;"><div class="box01"> <input type="text" id="tip" placeholder="You Know, for Search~"><i class="search_icon"></i></div></a></div></div></div></nav><section class="section"><div class="container"><div class="columns"><div class="column is-8-tablet is-8-desktop is-9-widescreen has-order-2 column-main"><div class="card"><div class="card-content article"><div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto"><div class="level-left"> <span style="color:#7a7a7a!important">写于2018-01-01 12:39:04&nbsp;&nbsp;&nbsp;</span><div class="level-item"> <a class="has-link-grey -link" href="/categories/Elasticsearch/">Elasticsearch</a></div> <span class="level-item has-text-grey">25 分钟 读完 (大约 3770 个字)</span><span class="level-item has-text-grey" id="busuanzi_container_page_pv"><i class="far fa-eye"></i> <span id="busuanzi_value_page_pv">0</span>次访问</span></div></div><h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal"> Elasticsearch初探</h1><div class="content"><p>[TOC]</p><h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>最近这个好火, 简单体验下</p><a id="more"></a><h3 id="一-安装和启动"><a href="#一-安装和启动" class="headerlink" title="一: 安装和启动"></a>一: 安装和启动</h3><h4 id="ElasticSearch下载"><a href="#ElasticSearch下载" class="headerlink" title="ElasticSearch下载"></a>ElasticSearch下载</h4><blockquote><p>官方地址: <a href="https://www.elastic.co/cn/downloads/elasticsearch" target="_blank" rel="noopener">https://www.elastic.co/cn/downloads/elasticsearch</a></p><p>github: <a href="https://github.com/elastic/elasticsearch" target="_blank" rel="noopener">https://github.com/elastic/elasticsearch</a></p></blockquote><p>下载或clone后解压</p><h4 id="单实例节点启动"><a href="#单实例节点启动" class="headerlink" title="单实例节点启动"></a>单实例节点启动</h4><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#</span> cd elasticsearch目录下</span><br><span class="line">bin/elasticsearch</span><br><span class="line">bin/elasticsearch -d # 后台启动</span><br></pre></td></tr></table></figure><p>默认端口9200, 启动完成后访问<code>http://ip:9200</code> 即可查看到节点信息</p><p>启动中我遇到两个错误:</p><p><strong>错误一:</strong></p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">can not run elasticsearch as root  </span><br><span class="line"><span class="hljs-meta">#</span> 不能以root用户启动</span><br></pre></td></tr></table></figure><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@01 bin]# groupadd xiefy</span><br><span class="line">[root@01 bin]# useradd xiefy -g xiefy -p 123123</span><br><span class="line">[root@01 bin]# chown -R xiefy:xiefy elasticsearch</span><br></pre></td></tr></table></figure><p><strong>错误二:</strong></p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ERROR: [2] bootstrap checks failed</span><br><span class="line">[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]</span><br><span class="line">[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]</span><br><span class="line"></span><br><span class="line">-- 错误[1]: max file descriptors过小</span><br><span class="line">-- 错误[2]: max_map_count过小, max_map_count文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量，系			  统默认是65530，修改成262144</span><br></pre></td></tr></table></figure><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#</span> 切换到root用户</span><br><span class="line">vi /etc/security/limits.conf</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#</span> 添加如下</span><br><span class="line">* soft nofile 65536</span><br><span class="line">* hard nofile 65536</span><br></pre></td></tr></table></figure><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#</span> 切换到root用户</span><br><span class="line">vi /etc/sysctl.conf</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#</span> 添加如下</span><br><span class="line">vm.max_map_count=655360</span><br><span class="line"><span class="hljs-meta">#</span> 重新加载配置文件或重启</span><br><span class="line">sysctl -p # 从配置文件“/etc/sysctl.conf”加载内核参数设置</span><br></pre></td></tr></table></figure><h4 id="elasticsearch-head插件安装"><a href="#elasticsearch-head插件安装" class="headerlink" title="elasticsearch-head插件安装"></a>elasticsearch-head插件安装</h4><blockquote><p> elasticsearch-head 是一个用于浏览Elastic Search集群并与之进行交互(操作和管理)的web界面</p><p> GitHub: <a href="https://github.com/mobz/elasticsearch-head" target="_blank" rel="noopener">https://github.com/mobz/elasticsearch-head</a></p><p> 要使用elasticsearch-head, 需要具备nodejs环境:</p></blockquote><p><strong>nodejs安装</strong></p><blockquote><p> 下载源码: <a href="https://nodejs.org/en/download/" target="_blank" rel="noopener">https://nodejs.org/en/download/</a></p></blockquote><p>安装方式有多种, 我用源码安装方式(包含npm)</p><ol><li><p>下载源码:</p><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://nodejs.org/dist/v8.9.4/node-v8.9.4.tar.gz</span><br></pre></td></tr></table></figure></li><li><p>解压源码:</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar xzvf node-v* &amp;&amp; cd node-v*</span><br></pre></td></tr></table></figure></li><li><p>安装必要的编译软件</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo yum install gcc gcc-c++</span><br></pre></td></tr></table></figure></li><li><p>编译</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">./configure</span><br><span class="line">make</span><br></pre></td></tr></table></figure></li><li><p>编译&amp;安装</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo make install</span><br></pre></td></tr></table></figure></li><li><p>查看版本</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">node --version</span><br><span class="line">npm -version</span><br></pre></td></tr></table></figure></li></ol><p>下载或克隆<code>elasticsearch-head</code>后, 进入<code>elasticsearch-head-master</code>目录:</p><ul><li><p><code>npm install</code></p><p>速度太慢可以使用淘宝镜像: <code>npm install -g cnpm --registry=https://registry.npm.taobao.org</code></p></li><li><p><code>npm run start</code></p></li><li><p>open <a href="http://localhost:9100/" target="_blank" rel="noopener">http://localhost:9100/</a></p></li></ul><p>这时可以访问到页面, 并没有监听到集群.</p><p>解决head插件和elasticsearch之间访问跨域问题.</p><p>修改elasticsearch目录下的<code>elasticsearch.yml</code></p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#</span> 加入以下内容</span><br><span class="line">http.cors.enabled: true</span><br><span class="line">http.cors.allow-origin: "*"</span><br></pre></td></tr></table></figure><p>然后: <a href="http://localhost:9100/" target="_blank" rel="noopener">http://localhost:9100/</a> 即可访问到管理页面.</p><h4 id="分布式安装启动"><a href="#分布式安装启动" class="headerlink" title="分布式安装启动"></a>分布式安装启动</h4><p>elasticsearch的横向扩展很容易: 这里建立一个主节点(node-master), 两个随从节点(node-1, node-2)</p><p>我提前拷贝了三个es:</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[xiefy@01 elk]$ ll</span><br><span class="line">total 33620</span><br><span class="line">-rw-r--r-- 1 root  root  33485703 Aug 17 22:42 elasticsearch-5.5.2.tar.gz</span><br><span class="line">drwxr-xr-x 7 root  root      4096 Jan  8 11:17 elasticsearch-head-master</span><br><span class="line">drwxr-xr-x 9 xiefy xiefy     4096 Jan  8 10:15 elasticsearch-master</span><br><span class="line">drwxr-xr-x 9 xiefy xiefy     4096 Jan  8 14:13 elasticsearch-node1</span><br><span class="line">drwxr-xr-x 9 xiefy xiefy     4096 Jan  8 14:16 elasticsearch-node2</span><br><span class="line">-rw-r--r-- 1 root  root    921421 Jan  8 11:14 master.zip</span><br></pre></td></tr></table></figure><p>分别配置三个es目录中的<code>config/elasticsearch.yml</code></p><p><strong>node-master</strong>:</p><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cluster.name: xiefy_elastic </span><br><span class="line">node.name: node-master </span><br><span class="line">node.master: true </span><br><span class="line">network.host: 0.0.0.0 </span><br><span class="line"></span><br><span class="line"># 除此之外, head插件需要连接到port: 9200的节点上, 还需要这个配置, </span><br><span class="line"># 用来允许 elasticsearch-head 运行时的跨域</span><br><span class="line">http.cors.enabled: true</span><br><span class="line">http.cors.allow-origin: &quot;*&quot;</span><br></pre></td></tr></table></figure><p><strong>node-1</strong>:</p><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cluster.name: xiefy_elastic </span><br><span class="line">node.name: node-1</span><br><span class="line">network.host: 0.0.0.0</span><br><span class="line">http.port: 9201 </span><br><span class="line">discovery.zen.ping.unicast.hosts: [&quot;127.0.0.1&quot;]</span><br></pre></td></tr></table></figure><p><strong>node-2</strong>: 参考node-1</p><p><strong>相关配置解释</strong>:</p><ul><li><p><code>cluster.name</code>: 集群名称, 默认是elasticsearch</p></li><li><p><code>node.name</code>: 节点名称, 默认随机分配</p></li><li><p><code>node.master</code>: 是否是主节点, 默认情况下可不写, 第一个起来的就是Master, 挂掉后会重新选举Master</p></li><li><p><code>network.host</code>: 默认情况下只允许本机通过localhost或127.0.0.1访问, 为了测试方便,</p><p>我需要远程访问所以配成了<code>0.0.0.0</code></p></li><li><p><code>http.port</code>: 默认为9200, 同一个服务器下启动多个es节点, 默认端口号会从9200默认递增1, 这里我手动指定了</p></li><li><p><code>discovery.zen.ping.unicast.hosts: [&quot;host1&quot;, &quot;host2&quot;]</code></p><p>Elasticsearch默认使用服务发现(Zen discovery)作为集群节点间发现和通信的机制, 当启动新节点时，通过这个ip列表进行节点发现，组建集群.</p></li></ul><p>分别启动三个es实例和head插件:</p><p>访问<code>http://ip:9100</code>:</p><p><img src="https://blog-md-pic-1259135436.cos.ap-chengdu.myqcloud.com/ELK/es%E5%B8%A6%E7%B4%A2%E5%BC%95%E7%9A%84%E9%9B%86%E7%BE%A4.png" alt="es三个节点集群"></p><h3 id="二-基础概念"><a href="#二-基础概念" class="headerlink" title="二: 基础概念"></a>二: 基础概念</h3><p>ElasticSearch与关系型数据库的一些术语比较</p><table><thead><tr><th>关系型数据库</th><th>Ela</th></tr></thead><tbody><tr><td>Database</td><td>Index</td></tr><tr><td>Table</td><td>Type</td></tr><tr><td>Row</td><td>Document</td></tr><tr><td>Column</td><td>Field</td></tr><tr><td>Schema</td><td>Mapping</td></tr><tr><td>Index</td><td>Everything is indexed</td></tr><tr><td>SQL`</td><td>Query DSL</td></tr><tr><td>select * from table…</td><td>GET http://…</td></tr><tr><td>update tables set…</td><td>PUT http://…</td></tr></tbody></table><p>Server:</p><ul><li>Node: 一个server实例</li><li>Cluster：多个node组成cluster</li><li>Shard：数据分片，一个index可能会有多个shards，不同shards可能在不同nodes</li><li>Replica：shard的备份，有一个primary shard，其余的叫做replica shards</li><li>Gateway：管理cluster状态信息</li></ul><h4 id="Shards-amp-Replicas"><a href="#Shards-amp-Replicas" class="headerlink" title="Shards &amp; Replicas"></a>Shards &amp; Replicas</h4><p>副本很重要, 主要有两个原因:</p><ul><li>它在分片/节点发生故障时来保障高可用性, 因此, 副本分片永远不会和主/原始分片分配在同一个节点中</li><li><p>它允许扩展搜索量和吞吐量, 因为可以在所有副本上并行执行搜索</p><p>可以在创建索引时为索引定义分片和副本的数量。<br>创建索引后，可以随时动态更改副本数，但不能再更改分片数。</p><blockquote><p>每个Elasticsearch分片都是Lucene索引, 在一个Lucene上的最大文档数量大约21亿(<code>Integer.MAX_VALUE-128</code> )</p></blockquote></li></ul><p>THE REST API:</p><p>elasticsearh提供了丰富的REST API来与集群交互, 这些API包括:</p><ul><li>检查你的集群，节点和索引的健康情况，状态和统计</li><li>管理你的集群，节点，索引数据和metadata</li><li>针对索引执行CURD（创建，读取，更新，删除）和搜索操作</li><li>执行高级的搜索操作，例如分页，排序，过滤，聚合，脚本等等。</li></ul><p>通过<code>_cat</code>可以查看到很多资源:</p><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">curl -XGET &apos;localhost:9200/_cat&apos;</span><br><span class="line"></span><br><span class="line">=^.^=</span><br><span class="line">/_cat/allocation</span><br><span class="line">/_cat/shards</span><br><span class="line">/_cat/shards/&#123;index&#125;</span><br><span class="line">/_cat/master</span><br><span class="line">/_cat/nodes</span><br><span class="line">/_cat/tasks</span><br><span class="line">/_cat/indices</span><br><span class="line">/_cat/indices/&#123;index&#125;</span><br><span class="line">/_cat/segments</span><br><span class="line">/_cat/segments/&#123;index&#125;</span><br><span class="line">/_cat/count</span><br><span class="line">/_cat/count/&#123;index&#125;</span><br><span class="line">/_cat/recovery</span><br><span class="line">/_cat/recovery/&#123;index&#125;</span><br><span class="line">/_cat/health</span><br><span class="line">/_cat/pending_tasks</span><br><span class="line">/_cat/aliases</span><br><span class="line">/_cat/aliases/&#123;alias&#125;</span><br><span class="line">/_cat/thread_pool</span><br><span class="line">/_cat/thread_pool/&#123;thread_pools&#125;</span><br><span class="line">/_cat/plugins</span><br><span class="line">/_cat/fielddata</span><br><span class="line">/_cat/fielddata/&#123;fields&#125;</span><br><span class="line">/_cat/nodeattrs</span><br><span class="line">/_cat/repositories</span><br><span class="line">/_cat/snapshots/&#123;repository&#125;</span><br><span class="line">/_cat/templates</span><br></pre></td></tr></table></figure><p>偷偷看一眼集群健康状态: <code>curl -XGET &#39;localhost:9200/_cat/health?v&#39;</code></p><p>节点的信息: <code>curl -XGET &#39;localhost:9200/_cat/nodes?v&amp;pretty&#39;</code></p><p>而关于和集群索引和文档的交互, 放在下一章.</p><h3 id="三-基础用法"><a href="#三-基础用法" class="headerlink" title="三: 基础用法"></a>三: 基础用法</h3><h4 id="索引创建"><a href="#索引创建" class="headerlink" title="索引创建"></a>索引创建</h4><ul><li><p>方式一: head插件可以直接新建/删除/查询索引(Index)</p></li><li><p>方式二: 通过rest api</p><figure class="highlight shell hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">#</span> 新建</span><br><span class="line">curl -X PUT 'localhost:9200/book'</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#</span> 删除</span><br><span class="line">curl -X DELETE 'localhost:9200/book'</span><br><span class="line"></span><br><span class="line"><span class="hljs-meta">#</span> 查看当前节点下所有Index</span><br><span class="line">curl -X GET 'http://localhost:9200/_cat/indices?v'</span><br></pre></td></tr></table></figure></li></ul><p>这样创建的Index是没有结构的. 可以看到索引信息的mappings:{}</p><p>下面来定义一个有结构映射的Index</p><blockquote><p>PUT <a href="http://ip:9200/people" target="_blank" rel="noopener">http://ip:9200/people</a></p></blockquote><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"settings"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"number_of_shards"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">		<span class="hljs-attr">"number_of_replicas"</span>: <span class="hljs-number">1</span></span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="hljs-attr">"mappings"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"man"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"properties"</span>: &#123;</span><br><span class="line">				<span class="hljs-attr">"name"</span>: &#123;<span class="hljs-attr">"type"</span>: <span class="hljs-string">"text"</span>&#125;,</span><br><span class="line">				<span class="hljs-attr">"country"</span>: &#123;<span class="hljs-attr">"type"</span>: <span class="hljs-string">"keyword"</span>&#125;,</span><br><span class="line">				<span class="hljs-attr">"age"</span>: &#123;<span class="hljs-attr">"type"</span>: <span class="hljs-string">"integer"</span>&#125;,</span><br><span class="line">				<span class="hljs-attr">"birthday"</span>: &#123;</span><br><span class="line">					<span class="hljs-attr">"type"</span>: <span class="hljs-string">"date"</span>,</span><br><span class="line">					<span class="hljs-attr">"format"</span>: <span class="hljs-string">"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="hljs-attr">"women"</span>: &#123;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>里面设置了分片数, 备份数, 一个Index和两个type的结构映射.</p><h4 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h4><blockquote><p>PUT <a href="http://47.94.210.157:9200/people/man/1" target="_blank" rel="noopener">http://47.94.210.157:9200/people/man/1</a></p><p>指定ID为1</p></blockquote><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"name"</span>: <span class="hljs-string">"伊布"</span>,</span><br><span class="line">	<span class="hljs-attr">"country"</span>: <span class="hljs-string">"瑞典"</span>,</span><br><span class="line">	<span class="hljs-attr">"age"</span>: <span class="hljs-number">30</span>,</span><br><span class="line">	<span class="hljs-attr">"birthday"</span>: <span class="hljs-string">"1988-12-12"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>如果不指定ID, 会生成为随机字符串, 此时需要改为POST方式 <code>POST http://47.94.210.157:9200/people/man</code></p><p>神奇的一点, es不会限制你在创建一个文档之前索引和类型必须存在, 不存在时, es会自动创建它.</p><h4 id="更新数据"><a href="#更新数据" class="headerlink" title="更新数据"></a>更新数据</h4><blockquote><p>POST <a href="http://47.94.210.157:9200/people/man/1/_update" target="_blank" rel="noopener">http://47.94.210.157:9200/people/man/1/_update</a> – 修改ID为1的文档</p></blockquote><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"doc"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西梅西很像很强"</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>还可以通过脚本方式更新:</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123; <span class="hljs-attr">"script"</span>: <span class="hljs-string">"ctx._source.age += 10"</span> &#125;</span><br></pre></td></tr></table></figure><p>这里是把年龄加10, <code>ctx.source</code>引用了当前文档.</p><h4 id="索引-替换文档"><a href="#索引-替换文档" class="headerlink" title="索引/替换文档"></a>索引/替换文档</h4><blockquote><p>PUT <a href="http://47.94.210.157:9200/football/man/1" target="_blank" rel="noopener">http://47.94.210.157:9200/football/man/1</a> – 修改ID为1的文档</p></blockquote><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&quot;name&quot;: &quot;伊布asdasadasds3333333333333333sd&quot;&#125;</span><br></pre></td></tr></table></figure><p>elasticsearch将会用一个新的文档取代（即重建索引）旧的文档</p><p><strong>删除数据</strong></p><p>删除文档: <code>DELETE http://47.94.210.157:9200/people/man/1</code></p><h4 id="查看数据"><a href="#查看数据" class="headerlink" title="查看数据"></a>查看数据</h4><ul><li>根据ID查询</li></ul><blockquote><p>GET <a href="http://ip:9200/people/man/1?pretty=true" target="_blank" rel="noopener">http://ip:9200/people/man/1?pretty=true</a></p></blockquote><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="hljs-attr">"_index"</span>: <span class="hljs-string">"people"</span>,</span><br><span class="line">  <span class="hljs-attr">"_type"</span>: <span class="hljs-string">"man"</span>,</span><br><span class="line">  <span class="hljs-attr">"_id"</span>: <span class="hljs-string">"2"</span>,</span><br><span class="line">  <span class="hljs-attr">"_version"</span>: <span class="hljs-number">1</span>,</span><br><span class="line">  <span class="hljs-attr">"found"</span>: <span class="hljs-literal">true</span>,</span><br><span class="line">  <span class="hljs-attr">"_source"</span>: &#123;</span><br><span class="line">    <span class="hljs-attr">"name"</span>: <span class="hljs-string">"伊布"</span>,</span><br><span class="line">    <span class="hljs-attr">"country"</span>: <span class="hljs-string">"瑞典"</span>,</span><br><span class="line">    <span class="hljs-attr">"age"</span>: <span class="hljs-number">34</span>,</span><br><span class="line">    <span class="hljs-attr">"birthday"</span>: <span class="hljs-string">"1972-12-12"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><code>found</code>字段表示查到与否</p><p> ​</p><ul><li>查询全部</li></ul><blockquote><p>GET <a href="http://ip:9200/Index/Type/_search" target="_blank" rel="noopener">http://ip:9200/Index/Type/_search</a></p></blockquote><p>或带排序带分页的查询:</p><blockquote><p>POST <a href="http://47.94.210.157:9200/people/_search" target="_blank" rel="noopener">http://47.94.210.157:9200/people/_search</a></p></blockquote><p>ES 默认 从0开始(from), 一次返回10条(size), 并按照_score字段倒排, 也可以自己指定</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 带排序带分页的查询</span><br><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123; <span class="hljs-attr">"match_all"</span>: &#123;&#125; &#125;,</span><br><span class="line">	<span class="hljs-attr">"sort"</span>: [&#123;</span><br><span class="line">		<span class="hljs-attr">"birthday"</span>: &#123;<span class="hljs-attr">"order"</span>: <span class="hljs-string">"desc"</span>&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	],</span><br><span class="line">	<span class="hljs-attr">"from"</span>: <span class="hljs-number">0</span>, </span><br><span class="line">	<span class="hljs-attr">"size"</span>: <span class="hljs-number">5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="hljs-attr">"took"</span>: <span class="hljs-number">5</span>,</span><br><span class="line">  <span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,</span><br><span class="line">  <span class="hljs-attr">"_shards"</span>: &#123;</span><br><span class="line">    <span class="hljs-attr">"total"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">    <span class="hljs-attr">"successful"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">    <span class="hljs-attr">"failed"</span>: <span class="hljs-number">0</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="hljs-attr">"hits"</span>: &#123;</span><br><span class="line">    <span class="hljs-attr">"total"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">    <span class="hljs-attr">"max_score"</span>: <span class="hljs-number">1</span>,</span><br><span class="line">    <span class="hljs-attr">"hits"</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="hljs-attr">"_index"</span>: <span class="hljs-string">"people"</span>,</span><br><span class="line">        <span class="hljs-attr">"_type"</span>: <span class="hljs-string">"man"</span>,</span><br><span class="line">        <span class="hljs-attr">"_id"</span>: <span class="hljs-string">"2"</span>,</span><br><span class="line">        <span class="hljs-attr">"_score"</span>: <span class="hljs-number">1</span>,</span><br><span class="line">        <span class="hljs-attr">"_source"</span>: &#123;</span><br><span class="line">          <span class="hljs-attr">"name"</span>: <span class="hljs-string">"伊布"</span>,</span><br><span class="line">          <span class="hljs-attr">"country"</span>: <span class="hljs-string">"瑞典"</span>,</span><br><span class="line">          <span class="hljs-attr">"age"</span>: <span class="hljs-number">34</span>,</span><br><span class="line">          <span class="hljs-attr">"birthday"</span>: <span class="hljs-string">"1972-12-12"</span></span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      ....</span><br><span class="line">      ....</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight plain hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 返回结果解释</span><br><span class="line">- took: 耗时(单位毫秒)</span><br><span class="line">- timed_out: 是否超时</span><br><span class="line">- hits: 命中的记录数组 </span><br><span class="line">  - total: 返回的记录数</span><br><span class="line">  - max_score: 最高匹配度分数</span><br><span class="line">  - hits: 记录数组</span><br><span class="line">    - _score: 匹配度</span><br></pre></td></tr></table></figure><ul><li>关键字查询</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西"</span>&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>注意</strong>: 这里是模糊匹配查询, 例如查询的值是”西2”, 那么会查询所有记录name有”西”和name有”2”的.</p><p>关于查询多个关键字之间的逻辑运算:</p><p>如果这样写, 两个关键字会被认为是 <code>or</code>的关系来查询</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"西 布"</span>&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>如果是<code>and</code>关系来搜索, 需要布尔查询</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"bool"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"must"</span>: [</span><br><span class="line">				&#123;<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"西"</span>&#125;&#125; ,</span><br><span class="line">				&#123;<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"2"</span>&#125;&#125; </span><br><span class="line">			]</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="聚合查询"><a href="#聚合查询" class="headerlink" title="聚合查询"></a>聚合查询</h4><blockquote><p>POST <a href="http://47.94.210.157:9200/people/_search" target="_blank" rel="noopener">http://47.94.210.157:9200/people/_search</a></p></blockquote><ul><li>分组聚合</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"aggs"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"group_by_age"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"terms"</span>: &#123;<span class="hljs-attr">"field"</span>: <span class="hljs-string">"age"</span>&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>返回结果中, 除了有hits数组, 还有聚合查询的结果:</p><p>在单个请求中就可以同时查询数据和进行多次聚合运算是非常有意义的, 他可以降低网络请求的次数</p><figure class="highlight hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"># 聚合查询结果</span><br><span class="line">"aggregations": &#123;</span><br><span class="line">	"group_by_age": &#123;</span><br><span class="line">	  "doc_count_error_upper_bound": 0,</span><br><span class="line">	  "sum_other_doc_count": 0,</span><br><span class="line">	  "buckets": [</span><br><span class="line">		&#123;</span><br><span class="line">		  <span class="hljs-attr">"key"</span>: <span class="hljs-number">24</span>,</span><br><span class="line">		  <span class="hljs-attr">"doc_count"</span>: <span class="hljs-number">2</span></span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">		  <span class="hljs-attr">"key"</span>: <span class="hljs-number">32</span>,</span><br><span class="line">		  <span class="hljs-attr">"doc_count"</span>: <span class="hljs-number">1</span></span><br><span class="line">		&#125;</span><br><span class="line">	  ]</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>支持多个聚合, 聚合结果也会返回多个:</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"aggs"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"group_by_age"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"terms"</span>: &#123;<span class="hljs-attr">"field"</span>: <span class="hljs-string">"age"</span>&#125;</span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="hljs-attr">"group_by_age"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"terms"</span>: &#123;<span class="hljs-attr">"field"</span>: <span class="hljs-string">"age"</span>&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>其他功能函数</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"aggs"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"tongji_age"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"stats"</span>: &#123;<span class="hljs-attr">"field"</span>: <span class="hljs-string">"age"</span>&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><code>stats</code>指定计算字段, 返回结果包括了总数, 最小值, 最大值, 平均值和求和</p><figure class="highlight hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">"aggregations": &#123;</span><br><span class="line">   "tongji_age": &#123;</span><br><span class="line">     "count": 3,</span><br><span class="line">     "min": 24,</span><br><span class="line">     "max": 32,</span><br><span class="line">     "avg": 26.666666666666668,</span><br><span class="line">     "sum": 80</span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure><p>也可指定某种类型的计算</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"aggs"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"tongji_age"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"sum"</span>: &#123;<span class="hljs-attr">"field"</span>: <span class="hljs-string">"age"</span>&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>返回结果</p><figure class="highlight hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">"aggregations": &#123;</span><br><span class="line">    "tongji_age": &#123;</span><br><span class="line">      "value": 80</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure><h3 id="四-高级查询"><a href="#四-高级查询" class="headerlink" title="四: 高级查询"></a>四: 高级查询</h3><p>分为<strong>子条件查询</strong>和<strong>复合条件</strong>查询:</p><p>类型:</p><ul><li><strong>全文本查询</strong>: 针对文本类型数据</li><li><strong>字段级别查询</strong>: 针对结构化数据, 如日期, 数字</li></ul><h4 id="文本查询"><a href="#文本查询" class="headerlink" title="文本查询"></a>文本查询</h4><ul><li>模糊匹配</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"西2"</span>&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>短语匹配</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"match_phrase"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"西2"</span>&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>多个字段匹配</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"multi_match"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"query"</span>: <span class="hljs-string">"瑞典"</span>,</span><br><span class="line">			<span class="hljs-attr">"fields"</span>: [<span class="hljs-string">"name"</span>, <span class="hljs-string">"country"</span>]</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>语法查询</strong>: 根据语法规则查询:</p><ul><li>带有布尔逻辑的查询</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"query_string"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"query"</span>: <span class="hljs-string">"(西 AND 梅) OR 布"</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>query_string 查询多个字段</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"query_string"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"query"</span>: <span class="hljs-string">"西梅 OR  瑞典"</span>,</span><br><span class="line">			<span class="hljs-attr">"fields"</span>: [<span class="hljs-string">"country"</span>, <span class="hljs-string">"name"</span>]</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="结构化数据查询"><a href="#结构化数据查询" class="headerlink" title="结构化数据查询"></a>结构化数据查询</h4><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;<span class="hljs-attr">"term"</span>: &#123; <span class="hljs-attr">"age"</span>: <span class="hljs-number">24</span>&#125;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>带范围的查询</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"range"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"birthday"</span>: &#123;</span><br><span class="line">				<span class="hljs-attr">"gte"</span>: <span class="hljs-string">"1980-01-01"</span>,</span><br><span class="line">				<span class="hljs-attr">"lte"</span>: <span class="hljs-string">"now"</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="子条件查询"><a href="#子条件查询" class="headerlink" title="子条件查询"></a>子条件查询</h4><p>Filter Context: 用来做数据过滤, 在查询过程中, 只判断该文档是否满足条件(y or not)</p><p>Filter和Query的区别?</p><p>Filter要结合bool使用, 查询结果会放入缓存中, 速度较Query更快</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"bool"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"filter"</span>: &#123;</span><br><span class="line">				<span class="hljs-attr">"term"</span>: &#123; <span class="hljs-attr">"age"</span>: <span class="hljs-number">24</span> &#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="复合查询"><a href="#复合查询" class="headerlink" title="复合查询"></a>复合查询</h4><ul><li>固定分数查询</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西"</span>&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>可以看到查询的结果, 每条数据的<code>_score</code>不同, 代表了与查询值的匹配程度的分数.</p><p>但查询不一定都需要产生文档得分，特别在过滤文档集合的时候。为了避免不必要的文档得分计算，Elasticsearch会检查这种情况并自动的优化这种查询。</p><p>例如在<code>bool</code>查询中返回年龄范围在20-50的文档, 对我来说这个范围内的文档都是等价的, 即他们的相关度都是一样的(filter子句查询，不会改变得分).</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"constant_score"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"filter"</span>: &#123;</span><br><span class="line">				<span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西"</span>&#125;</span><br><span class="line">			&#125;,</span><br><span class="line">			<span class="hljs-attr">"boost"</span>: <span class="hljs-number">2</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>可以看到查询结果, 每条数据的<code>_score</code>都为2, 如果不指定<code>boost</code>则默认为1</p><ul><li>布尔查询</li></ul><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"bool"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"should"</span>: [</span><br><span class="line">				&#123; <span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西"</span>&#125;&#125;,</span><br><span class="line">				&#123; <span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"country"</span>: <span class="hljs-string">"阿"</span>&#125;&#125;</span><br><span class="line">			]</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>这里两个match之间是或的逻辑关系. <code>should</code> 如果改为 <code>must</code> 代表与逻辑.</p><p>我们也可以把<code>must</code>，<code>should</code>，<code>must_not</code>同时组合到<code>bool</code>子句。此外，我们也可以组合<code>bool</code> 到任何一个<code>bool</code>子句中，实现复杂的多层<code>bool</code>子句嵌套逻辑。</p><p>再加一层Filter, 只有age=32的能返回</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"bool"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"must"</span>: [</span><br><span class="line">				&#123; <span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"name"</span>: <span class="hljs-string">"梅西"</span>&#125; &#125;,</span><br><span class="line">				&#123; <span class="hljs-attr">"match"</span>: &#123;<span class="hljs-attr">"country"</span>: <span class="hljs-string">"阿根廷"</span>&#125;&#125;</span><br><span class="line">			],</span><br><span class="line">			<span class="hljs-attr">"filter"</span>: [&#123;</span><br><span class="line">				<span class="hljs-attr">"term"</span>: &#123;</span><br><span class="line">					<span class="hljs-attr">"age"</span>: <span class="hljs-number">32</span></span><br><span class="line">				&#125;</span><br><span class="line">			&#125;]</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>country=阿根廷的不返回:</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="hljs-attr">"query"</span>: &#123;</span><br><span class="line">		<span class="hljs-attr">"bool"</span>: &#123;</span><br><span class="line">			<span class="hljs-attr">"must_not"</span>: &#123;</span><br><span class="line">				<span class="hljs-attr">"term"</span>: &#123;<span class="hljs-attr">"country"</span>: <span class="hljs-string">"阿根廷"</span>&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="五-关于中文分词"><a href="#五-关于中文分词" class="headerlink" title="五: 关于中文分词"></a>五: 关于中文分词</h3><p>为什么需要中文分词?</p><p>首先看一下默认的分词规则.</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"># 英文</span><br><span class="line">GET http://47.94.210.157:9200/_analyze?analyzer=standard&amp;pretty=true&amp;text=hello world, elasticsearch</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="hljs-attr">"tokens"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"hello"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">0</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">5</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;ALPHANUM&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"world"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">6</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">11</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;ALPHANUM&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"elasticsearch"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">13</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">26</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;ALPHANUM&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">2</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>可以看到, 英文的默认分词是根据标点符号和空格默认来分的.</p><p>再看看中文的:</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">GET http://47.94.210.157:9200/_analyze?analyzer=standard&amp;pretty=true&amp;text=你好,啊</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="hljs-attr">"tokens"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"你"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">0</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">1</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;IDEOGRAPHIC&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"好"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">1</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">2</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;IDEOGRAPHIC&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"啊"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">4</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"&lt;IDEOGRAPHIC&gt;"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">2</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>可以看到ES对中文的分词并不智能, 是将汉字全部分开了, 所以引入中文分词.</p><h4 id="IK"><a href="#IK" class="headerlink" title="IK"></a>IK</h4><blockquote><p>IK: <a href="https://github.com/medcl/elasticsearch-analysis-ik" target="_blank" rel="noopener">https://github.com/medcl/elasticsearch-analysis-ik</a></p><p>The IK Analysis plugin integrates Lucene IK analyzer into elasticsearch, support customized dictionary.</p></blockquote><p><strong>安装</strong></p><p>1.download or compile</p><ul><li><p>optional 1 - download pre-build package from here: <a href="https://github.com/medcl/elasticsearch-analysis-ik/releases" target="_blank" rel="noopener">https://github.com/medcl/elasticsearch-analysis-ik/releases</a></p><p>unzip plugin to folder <code>your-es-root/plugins/</code></p></li><li><p>optional 2 - use elasticsearch-plugin to install ( version &gt; v5.5.1 ):</p><p><code>./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.1/elasticsearch-analysis-ik-6.2.1.zip</code></p></li></ul><p>2.restart elasticsearch</p><p>两种安装方式, 任选其一, 注意版本就好</p><p>Github里有<code>Quick Example</code> 可以看下怎么使用</p><p>需要在建立索引时指定ik分词器, 建立索引和搜索索引字段都需要指定, 例如:</p><p><code>&quot;analyzer&quot;: &quot;ik_max_word&quot;</code>和<code>&quot;search_analyzer&quot;: &quot;ik_max_word&quot;</code></p><p>IK提供两种分词规则:</p><ul><li>ik_max_word: 会将文本做最细粒度的拆分，比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”，会穷尽各种可能的组合；</li><li>ik_smart: 会做最粗粒度的拆分，比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。</li></ul><p>除此之外, IK也支持扩展自定义词典, 以及热更新.</p><figure class="highlight json hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"># Test</span><br><span class="line">GET http://47.94.210.157:9200/_analyze?analyzer=ik_max_word&amp;pretty=true&amp;text=你好,啊</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  <span class="hljs-attr">"tokens"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"你好"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">0</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">2</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"CN_WORD"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="hljs-attr">"token"</span>: <span class="hljs-string">"啊"</span>,</span><br><span class="line">      <span class="hljs-attr">"start_offset"</span>: <span class="hljs-number">3</span>,</span><br><span class="line">      <span class="hljs-attr">"end_offset"</span>: <span class="hljs-number">4</span>,</span><br><span class="line">      <span class="hljs-attr">"type"</span>: <span class="hljs-string">"CN_CHAR"</span>,</span><br><span class="line">      <span class="hljs-attr">"position"</span>: <span class="hljs-number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="六-Spring-Boot-集成-Elastic-Search"><a href="#六-Spring-Boot-集成-Elastic-Search" class="headerlink" title="六: Spring Boot 集成 Elastic Search"></a>六: Spring Boot 集成 Elastic Search</h3><h4 id="版本参考"><a href="#版本参考" class="headerlink" title="版本参考"></a>版本参考</h4><table><thead><tr><th>Spring Boot Version (x)</th><th>Spring Data Elasticsearch Version (y)</th><th>Elasticsearch Version (z)</th></tr></thead><tbody><tr><td>x &lt;= 1.3.5</td><td>y &lt;= 1.3.4</td><td>z &lt;= 1.7.2*</td></tr><tr><td>x &gt;= 1.4.x</td><td>2.0.0 &lt;=y &lt; 5.0.0**</td><td>2.0.0 &lt;= z &lt; 5.0.0**</td></tr></tbody></table><table><thead><tr><th>服务器集群ES版本</th><th>5.5.2</th></tr></thead><tbody><tr><td>Spring boot</td><td>1.5.9.RELEASE</td></tr><tr><td>Elastic Search</td><td>5.5.2</td></tr><tr><td>log4j-core</td><td>2.7</td></tr></tbody></table><h4 id="集成步骤"><a href="#集成步骤" class="headerlink" title="集成步骤"></a>集成步骤</h4><ol><li><p>引入Maven依赖:</p><figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span></span><br><span class="line">	<span class="hljs-tag">&lt;<span class="hljs-name">log4j-core.version</span>&gt;</span>2.7<span class="hljs-tag">&lt;/<span class="hljs-name">log4j-core.version</span>&gt;</span></span><br><span class="line">	<span class="hljs-tag">&lt;<span class="hljs-name">elasticsearch-version</span>&gt;</span>5.5.2<span class="hljs-tag">&lt;/<span class="hljs-name">elasticsearch-version</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span></span><br><span class="line">	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.elasticsearch.client<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span></span><br><span class="line">	<span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>transport<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span></span><br><span class="line">	<span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;elasticsearch.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">&lt;!--</span></span><br><span class="line"><span class="hljs-comment">&lt;dependency&gt;</span></span><br><span class="line"><span class="hljs-comment">	&lt;groupId&gt;org.elasticsearch&lt;/groupId&gt;</span></span><br><span class="line"><span class="hljs-comment">	&lt;artifactId&gt;elasticsearch&lt;/artifactId&gt;</span></span><br><span class="line"><span class="hljs-comment">	&lt;version&gt;$&#123;elasticsearch-version&#125;&lt;/version&gt;</span></span><br><span class="line"><span class="hljs-comment">&lt;/dependency&gt;</span></span><br><span class="line"><span class="hljs-comment">--&gt;</span></span><br></pre></td></tr></table></figure><p><strong>注意</strong>: transport中依赖了elasticsearch, 但默认是<code>2.4.6</code>版本, 需要指定下elasticsearch的版本<code>5.5.2</code></p></li></ol><ol start="2"><li><p>也可以直接引入:</p><figure class="highlight xml hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span></span><br><span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span></span><br><span class="line">  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-data-elasticsearch<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span></span><br><span class="line"><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>但是<code>spring-boot-starter-data-elasticsearch</code>只支持到<code>2.4.x</code>版本的es.</p><p>如果使用<code>5.x.x</code>版本ES, 就用上面那种方式单独引入ES依赖.</p><p>​</p></li></ol><ol start="2"><li><p>添加配置类</p><figure class="highlight java hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-meta">@Configuration</span></span><br><span class="line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ElasticSearchConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">/** 集群host */</span></span><br><span class="line">    <span class="hljs-meta">@Value</span>(<span class="hljs-string">"$&#123;spring.data.elasticsearch.cluster-nodes&#125;"</span>)</span><br><span class="line">    <span class="hljs-keyword">private</span> String clusterNodes;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-comment">/** 集群名称 */</span></span><br><span class="line">    <span class="hljs-meta">@Value</span>(<span class="hljs-string">"$&#123;spring.data.elasticsearch.cluster-name&#125;"</span>)</span><br><span class="line">    <span class="hljs-keyword">private</span> String clusterName;</span><br><span class="line"></span><br><span class="line">    <span class="hljs-meta">@Bean</span></span><br><span class="line">    <span class="hljs-function"><span class="hljs-keyword">public</span> TransportClient <span class="hljs-title">client</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> UnknownHostException</span>&#123;</span><br><span class="line"></span><br><span class="line">        InetSocketTransportAddress node = <span class="hljs-keyword">new</span> InetSocketTransportAddress(</span><br><span class="line">                InetAddress.getByName(clusterNodes), <span class="hljs-number">9300</span></span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        Settings settings = Settings.builder().put(<span class="hljs-string">"cluster.name"</span>, clusterName).build();</span><br><span class="line"></span><br><span class="line">        TransportClient client = <span class="hljs-keyword">new</span> PreBuiltTransportClient(settings);</span><br><span class="line">        client.addTransportAddress(node);</span><br><span class="line">        <span class="hljs-keyword">return</span> client;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><code>application.properties</code>中配置:</p><ul><li><code>spring.data.elasticsearch.cluster-nodes=xxx</code></li><li><code>spring.data.elasticsearch.cluster-name=xxx</code></li></ul></li></ol><h4 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h4><p>简单的CRUL操作:</p><blockquote><p>github: <a href="https://github.com/thank037/elasticsearch_demo.git" target="_blank" rel="noopener">https://github.com/thank037/elasticsearch_demo.git</a></p></blockquote><p><code>@Link com.thank.elasticsearch.TestElasticSearchCRUD.java</code></p></div><ul class="post-copyright"><li><strong>本文标题：</strong><a href="https://xiefayang.com/2018/01/01/Elasticsearch初探/">Elasticsearch初探</a></li><li><strong>本文作者：</strong><a href="https://xiefayang.com">i蝸居年華_谢谢谢</a></li><li><strong>本文链接：</strong><a href="https://xiefayang.com/2018/01/01/Elasticsearch初探/">https://xiefayang.com/2018/01/01/Elasticsearch初探/</a></li><li><strong>发布时间：</strong>2018-01-01</li><li><strong>版权声明：</strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li></ul><div class="level is-size-7 is-uppercase"><div class="level-start"><div class="level-item"> <span class="is-size-6 has-text-grey has-mr-7">#</span> <a class="has-link-grey -link" href="/tags/ELK/">ELK</a>, <a class="has-link-grey -link" href="/tags/Elasticsearch/">Elasticsearch</a></div></div></div><div class="social-share"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js@1.0.16/dist/js/social-share.min.js"></script></div></div><div class="card"><div class="card-content"><h3 class="menu-label has-text-centered">坚持原创技术分享，您的支持将鼓励我继续创作！</h3><div class="buttons is-centered"><a class="button is-info donate"><span class="icon is-small"><i class="fab fa-alipay"></i></span> <span>支付宝</span><div class="qrcode"><img src="/images/no2.jpg" alt="支付宝"></div></a><a class="button is-success donate"><span class="icon is-small"><i class="fab fa-weixin"></i></span> <span>微信</span><div class="qrcode"><img src="/images/no.gif" alt="微信"></div></a></div></div></div><div class="card card-transparent"><div class="level post-navigation is-flex-wrap is-mobile"><div class="level-start"><a class="level level-item has-link-grey article-nav-prev" href="/2018/06/01/Spring Data JPA 源码分析 - 方法命名查询/"><i class="level-item fas fa-chevron-left"></i> <span class="level-item">Spring Data JPA源码分析-方法命名查询</span></a></div><div class="level-end"> <a class="level level-item has-link-grey article-nav-next" href="/2017/12/25/通关Githug(二)/"><span class="level-item">通关Githug(二)</span><i class="level-item fas fa-chevron-right"></i></a></div></div></div><div class="card"><div class="card-content"><h3 class="title is-5 has-text-weight-normal">评论</h3><div id="valine-thread" class="content"></div><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="/js/Valine.min.js"></script><script>new Valine({el:"#valine-thread",notify:!1,verify:!1,app_id:"yVPV2fAKC1B90Xnh2JjuPEAb-gzGzoHsz",app_key:"1i9DgF7j75Uq6OuM23etjjE9",placeholder:"Just go go, 支持Markdown~"})</script></div></div></div><div class="column is-4-tablet is-4-desktop is-3-widescreen has-order-1 column-left"><div class="card widget"><div class="card-content"><nav class="level"><div class="level-item has-text-centered"><div> <img class="image is-128x128 has-mb-6" src="/images/avatar.png" alt="i蝸居年華_谢谢谢" style="border-radius:50%"><p class="is-size-4 is-block"> i蝸居年華_谢谢谢</p><p class="is-size-6 is-block"> Just for fun</p><p class="is-size-6 is-flex is-flex-center has-text-grey"><i class="fas fa-map-marker-alt has-mr-7"></i> <span>Beijing, China</span></p></div></div></nav><nav class="level is-mobile"><div class="level-item has-text-centered is-marginless"><div><p class="heading"> 文章</p><p class="title has-text-weight-normal"> 49</p></div></div><div class="level-item has-text-centered is-marginless"><div><p class="heading"> 分类</p><p class="title has-text-weight-normal"> 13</p></div></div><div class="level-item has-text-centered is-marginless"><div><p class="heading"> 标签</p><p class="title has-text-weight-normal"> 32</p></div></div></nav><div class="level"> <a class="level-item button is-link is-rounded" href="https://github.com/thank037" target="_blank">关注我</a></div><div class="level is-mobile"> <a class="level-item button is-white is-marginless" target="_blank" title="github" href="https://github.com/thank037" name><img src="/images/github.svg" style="width:20px;height:20px"></a> <a class="level-item button is-white is-marginless" target="_blank" title="gitee" href="https://gitee.com/thank037" name><img src="/images/gitee.svg" style="width:20px;height:20px"></a> <a class="level-item button is-white is-marginless" target="_blank" title="weibo" href="https://weibo.com/u/2139090054" name><img src="/images/weibo.svg" style="width:20px;height:20px"></a> <a class="level-item button is-white is-marginless" target="_blank" title="email" href="mailto:coderthank@163.com" name><img src="/images/email.svg" style="width:20px;height:20px"></a></div></div></div><div class="card widget column-left is-sticky" id="toc"><div class="card-content"><div class="menu"><h3 class="menu-label"> 文章目录</h3><ul class="menu-list"><li> <a class="is-flex" href="#前言"><span class="has-mr-6">1</span> <span>前言</span></a></li><li> <a class="is-flex" href="#一-安装和启动"><span class="has-mr-6">2</span> <span>一: 安装和启动</span></a><ul class="menu-list"><li> <a class="is-flex" href="#ElasticSearch下载"><span class="has-mr-6">2.1</span> <span>ElasticSearch下载</span></a></li><li> <a class="is-flex" href="#单实例节点启动"><span class="has-mr-6">2.2</span> <span>单实例节点启动</span></a></li><li> <a class="is-flex" href="#elasticsearch-head插件安装"><span class="has-mr-6">2.3</span> <span>elasticsearch-head插件安装</span></a></li><li> <a class="is-flex" href="#分布式安装启动"><span class="has-mr-6">2.4</span> <span>分布式安装启动</span></a></li></ul></li><li> <a class="is-flex" href="#二-基础概念"><span class="has-mr-6">3</span> <span>二: 基础概念</span></a><ul class="menu-list"><li> <a class="is-flex" href="#Shards-amp-Replicas"><span class="has-mr-6">3.1</span> <span>Shards &amp; Replicas</span></a></li></ul></li><li> <a class="is-flex" href="#三-基础用法"><span class="has-mr-6">4</span> <span>三: 基础用法</span></a><ul class="menu-list"><li> <a class="is-flex" href="#索引创建"><span class="has-mr-6">4.1</span> <span>索引创建</span></a></li><li> <a class="is-flex" href="#插入数据"><span class="has-mr-6">4.2</span> <span>插入数据</span></a></li><li> <a class="is-flex" href="#更新数据"><span class="has-mr-6">4.3</span> <span>更新数据</span></a></li><li> <a class="is-flex" href="#索引-替换文档"><span class="has-mr-6">4.4</span> <span>索引/替换文档</span></a></li><li> <a class="is-flex" href="#查看数据"><span class="has-mr-6">4.5</span> <span>查看数据</span></a></li><li> <a class="is-flex" href="#聚合查询"><span class="has-mr-6">4.6</span> <span>聚合查询</span></a></li></ul></li><li> <a class="is-flex" href="#四-高级查询"><span class="has-mr-6">5</span> <span>四: 高级查询</span></a><ul class="menu-list"><li> <a class="is-flex" href="#文本查询"><span class="has-mr-6">5.1</span> <span>文本查询</span></a></li><li> <a class="is-flex" href="#结构化数据查询"><span class="has-mr-6">5.2</span> <span>结构化数据查询</span></a></li><li> <a class="is-flex" href="#子条件查询"><span class="has-mr-6">5.3</span> <span>子条件查询</span></a></li><li> <a class="is-flex" href="#复合查询"><span class="has-mr-6">5.4</span> <span>复合查询</span></a></li></ul></li><li> <a class="is-flex" href="#五-关于中文分词"><span class="has-mr-6">6</span> <span>五: 关于中文分词</span></a><ul class="menu-list"><li> <a class="is-flex" href="#IK"><span class="has-mr-6">6.1</span> <span>IK</span></a></li></ul></li><li> <a class="is-flex" href="#六-Spring-Boot-集成-Elastic-Search"><span class="has-mr-6">7</span> <span>六: Spring Boot 集成 Elastic Search</span></a><ul class="menu-list"><li> <a class="is-flex" href="#版本参考"><span class="has-mr-6">7.1</span> <span>版本参考</span></a></li><li> <a class="is-flex" href="#集成步骤"><span class="has-mr-6">7.2</span> <span>集成步骤</span></a></li><li> <a class="is-flex" href="#测试用例"><span class="has-mr-6">7.3</span> <span>测试用例</span></a></li></ul></li></ul></div></div></div><div class="column-right-shadow is-hidden-widescreen is-sticky"></div></div></div></div></section><footer class="footer"><div class="container"><div class="level"><div class="level-start has-text-centered-mobile"> <a class="footer-logo is-block has-mb-6" href="/">i蝸居年華_谢谢谢</a><p class="is-size-7"> &copy; 2016—2021 i蝸居年華_谢谢谢&nbsp; 由<a href="http://hexo.io/" target="_blank">Hexo</a> 强力驱动<br> <span id="busuanzi_container_site_uv">您是第<span id="busuanzi_value_site_uv">0</span>个访客</span></p></div><div class="level-end"><div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle"><p class="control"><a class="button is-white is-large" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a></p><p class="control"><a class="button is-white is-large" target="_blank" title="Attribution 4.0 International" href="https://creativecommons.org/licenses/by/4.0/"><i class="fab fa-creative-commons-by"></i></a></p><p class="control"><a class="button is-white is-large" target="_blank" title="Download on GitHub" href="https://github.com/thank037"><i class="fab fa-github"></i></a></p></div></div></div></div></footer><script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script><script>moment.locale("zh-CN")</script><script src="/js/animation.js"></script><script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer="defer"></script><script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer="defer"></script><script src="/js/gallery.js" defer="defer"></script><div id="outdated"><h6>Your browser is out-of-date!</h6><p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update my browser now</a></p><p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p></div><script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer="defer"></script><script>document.addEventListener("DOMContentLoaded",function(){outdatedBrowser({bgColor:"#f25648",color:"#ffffff",lowerThan:"flex"})})</script><script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer="defer"></script><script>document.addEventListener("DOMContentLoaded",function(){MathJax.Hub.Config({"HTML-CSS":{matchFontHeight:!1},SVG:{matchFontHeight:!1},CommonHTML:{matchFontHeight:!1},tex2jax:{inlineMath:[["$","$"],["\\(","\\)"]]}})})</script><a id="back-to-top" title="回到顶端" href="javascript:;"><i class="fas fa-chevron-up"></i></a><script src="/js/back-to-top.js" defer="defer"></script><script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer="defer"></script><script src="/js/clipboard.js" defer="defer"></script><script src="/js/main.js" defer="defer"></script><div class="searchbox ins-search"><div class="searchbox-container ins-search-container"><div class="searchbox-input-wrapper"> <input type="text" class="searchbox-input ins-search-input" placeholder="全文搜索站内文章~"><span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span></div><div class="searchbox-result-wrapper ins-section-wrapper"><div class="ins-section-container"></div></div></div></div><script>window.INSIGHT_CONFIG={TRANSLATION:{POSTS:"文章",PAGES:"页面",CATEGORIES:"分类",TAGS:"标签",UNTITLED:"(无标题)"},CONTENT_URL:"/content.json"}</script><script src="/js/insight.js" defer="defer"></script><link rel="stylesheet" href="/css/search.css"><link rel="stylesheet" href="/css/insight.css"><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,debug:!1,model:{jsonPath:"/live2dw/assets/hijiki.model.json"},display:{position:"right",width:150,height:300,mobile:{show:!0},react:{opacity:.7}},log:!1})</script></body></html>